<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { ElTable, FormInstance } from "element-plus"
import { formatDateTime } from "@/utils"
import { usePagination } from "@/hooks/usePagination"
import { Search, Refresh } from "@element-plus/icons-vue"
import { useRouter } from "vue-router"
import { getCompilationChildApi } from "@/api/combo"
const loading = ref(true)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
defineOptions({
  name: "childset"
})

const router = useRouter()
const pid = router.currentRoute.value.query.id //套餐合集ID
// #region 查
const tableTotal = ref<number>()
const tableData = ref<any>([])
const getApplet = () => {
  getCompilationChildApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    id: pid
  })
    .then((res: any) => {
      tableData.value = res.data.data.list
      tableTotal.value = res.data.data.total
      loading.value = false
    })
    .catch(() => {
      tableData.value = []
    })
}
// #endregion

// #region 搜索内容
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive<any>({
  packageName: ""
})
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getApplet()
  }
  paginationData.currentPage = 1
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getApplet()
  }
  paginationData.currentPage = 1
}
// #endregion
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getApplet, { immediate: true })
const strTo = (val: any) => {
  return eval(val).flat().join(",")
}
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="套餐名称">
          <el-input v-model.trim="searchData.packageName" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 中间部分 -->
    <el-card v-loading="loading" shadow="never">
      <!-- 顶部添加 -->
      <div class="middle-top">
        <div style="margin-top: 20px; font-size: 14px; color: red">
          级别定义:普通套餐级别默认0，保底套餐可自定义设置非0数值，订单轮转根据级别从小到大依次进行
        </div>
      </div>
      <!-- 中间渲染内容 -->
      <div class="table-center">
        <el-table :data="tableData">
          <el-table-column prop="create" label="创建时间" align="center">
            <template #default="scope">
              {{ formatDateTime(scope.row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="packageName" label="套餐名称" align="center" />
          <el-table-column prop="type" label="套餐类型" align="center">
            <template #default="scope">
              <span v-if="scope.row.type == 1">保底套餐</span>
              <span v-else>普通套餐</span>
            </template>
          </el-table-column>
          <el-table-column prop="grade" label="级别" align="center" />
          <el-table-column prop="province" label="发货地区" align="center">
            <template #default="scope">
              <span v-if="scope.row.type == 1">{{}}</span>
              <span v-else>{{ strTo(scope.row.province) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="packageArea" label="不发货地区" align="center">
            <template #default="scope">
              <span v-if="scope.row.type == 1">{{ strTo(scope.row.packageArea) }}</span>
              <span v-else>{{}}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :page-size="paginationData.pageSize"
          :total="tableTotal"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
// .box-card {
//   height: 70px;
//   margin-bottom: 20px;
// }
.search-wrapper {
  margin-bottom: 20px;
}
.middle-top {
  margin-bottom: 20px;
}
.table-center {
  margin-bottom: 20px;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
</style>
